package com.moli.iov.admin.service.impl;

import com.moli.iov.admin.config.FtpConfig;
import com.moli.iov.admin.service.TboxUpgradeFileService;
import com.moli.iov.base.model.dto.req.SoftVersionReqDto;
import com.moli.iov.base.model.dto.req.TboxUpgradeFileReqDto;
import com.moli.iov.base.model.dto.req.UpgradeFileQueryDto;
import com.moli.iov.base.model.dto.res.TboxUpgradeFileResDto;
import com.moli.iov.base.remote.ITboxUpgradeFileRemoteService;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.rest.RestResponse;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * The type Tbox upgrade file service.
 *
 * @author wanghualin
 * @date 2019-10-09 20:30:54
 */
@Service
public class TboxUpgradeFileServiceImpl implements TboxUpgradeFileService {
  @Reference(group = "${service.group}", check = false)
  private ITboxUpgradeFileRemoteService iTboxUpgradeFileRemoteService;

  @Resource
  FtpConfig ftpConfig;

  @Override
  public RestResponse softVersionList(SoftVersionReqDto reqDto){
    RestResponse<List<TboxUpgradeFileResDto>> res = iTboxUpgradeFileRemoteService.listSoftVersion(reqDto);
    return res;
  }

  private TboxUpgradeFileReqDto setFileUrl(TboxUpgradeFileReqDto reqDto){
    if(reqDto!=null && !StringUtils.isEmpty(reqDto.getFileUrl())){
      String ip = ftpConfig.getOutip();
      Integer port = ftpConfig.getPort();
      String userName = ftpConfig.getUsername();
      String pass  = ftpConfig.getPassword();
      String path = ftpConfig.getPath();
      String res = "ftp://"+userName+":"+pass+"@"+ip+":"+port+path+"/"+reqDto.getFileUrl();
      reqDto.setFileUrl(res);
    }
    return reqDto;
  }

  @Override
  public RestResponse saveUpgradeFile(TboxUpgradeFileReqDto reqDto) {
    reqDto = setFileUrl(reqDto);
    return iTboxUpgradeFileRemoteService.saveUpgradeFile(reqDto);
  }

  @Override
  public RestResponse updateUpgradeFile(TboxUpgradeFileReqDto reqDto) {
    reqDto = setFileUrl(reqDto);
    return iTboxUpgradeFileRemoteService.updateUpgradeFile(reqDto);
  }

  @Override
  public RestResponse deleteUpgradeFile(TboxUpgradeFileReqDto reqDto) {
    return iTboxUpgradeFileRemoteService.deleteUpgradeFile(reqDto);
  }

  @Override
  public RestResponse<PageExtend<TboxUpgradeFileResDto>>  listTboxUpgradeFile(UpgradeFileQueryDto reqDto) {
    return iTboxUpgradeFileRemoteService.listTboxUpgradeFile(reqDto);
  }

  @Override
  public RestResponse getTboxUpgradeFileDetails(TboxUpgradeFileReqDto dto) {
    return iTboxUpgradeFileRemoteService.getTboxUpgradeFileById(dto);
  }
}
